const {findRepair, findLink, findTower, bestFindClose, findFreeMainSite} = require("./Find");
const {
    create
} = require("./Create");
const {MyRoomList} = require("./Param");
const {changeMemory} = require("./Util");
//母巢运行逻辑
module.exports.createCreep = createCreep = (spawn) => {
    let room = spawn.room;
    if (room.memory.harvestList.length > 0) {
        let harvestList = room.memory.harvestList.filter(creep => !creep.spawning);
        if (harvestList.length > 0) {
            let harvest = harvestList[0];
            let status = create(harvest.name, "收集兵", harvest.level, spawn, harvest.source);
            changeMemory(status, harvest, room.memory.harvestList);
            return;
        }
    }
    if (room.memory.butlerList.length > 0) {
        let butlerList = room.memory.butlerList.filter(creep => !creep.spawning);
        if (butlerList.length > 0) {
            let butler = butlerList[0];
            let status = create(butler.name, "管家", butler.level, spawn);
            changeMemory(status, butler, room.memory.butlerList);
            return;
        }
    }
    if (room.memory.repairList.length > 0) {
        let repairList = room.memory.repairList.filter(creep => !creep.spawning);
        if (repairList.length > 0) {
            let repair = repairList[0];
            let status = create(repair.name, "维修兵", repair.level, spawn);
            changeMemory(status, repair, room.memory.repairList);
            return;
        }
    }
    if (room.memory.defenseList.length > 0) {
        if (room.find(FIND_HOSTILE_CREEPS).length > 0) {
            let defenseList = room.memory.defenseList.filter(creep => !creep.spawning);
            if (defenseList.length > 0) {
                let defense = defenseList[0];
                let status = create(defense.name, "防御兵", defense.level, spawn);
                changeMemory(status, defense, room.memory.defenseList);
                return;
            }
        } else {
            room.memory.defenseList = [];
        }

    }
    if (room.memory.guardList.length > 0) {
        let guardList = room.memory.guardList.filter(creep => !creep.spawning);
        if (guardList.length > 0) {
            let guard = guardList[0];
            let status = create(guard.name, "守护兵", guard.level, spawn, guard.guardRoom);
            changeMemory(status, guard, room.memory.guardList);
            return;
        }
    }
    if (room.memory.builderList.length > 0) {
        let buildList = room.memory.builderList.filter(creep => !creep.spawning);
        if (buildList.length > 0) {
            let build = buildList[0];
            let status = create(build.name, "建造兵", build.level, spawn);
            changeMemory(status, build, room.memory.builderList);
            return;
        }
    }
    if (room.memory.upList.length > 0) {
        let upList = room.memory.upList.filter(creep => !creep.spawning);
        if (upList.length > 0) {
            let up = upList[0];
            let status = create(up.name, "升级兵", up.level, spawn);
            changeMemory(status, up, room.memory.upList);
        }
        return;
    }
    if (room.memory.outHarvestList.length > 0) {
        let outHarvestList = room.memory.outHarvestList.filter(creep => !creep.spawning);
        if (outHarvestList.length > 0) {
            let outHarvest = outHarvestList[0];
            let status = create(outHarvest.name, "外矿收集兵", outHarvest.level, spawn, outHarvest.source, outHarvest.flagRoomName);
            changeMemory(status, outHarvest, room.memory.outHarvestList);
            return;
        }
    }
    if (room.memory.reserveList.length > 0) {
        let reserveList = room.memory.reserveList.filter(creep => !creep.spawning);
        if (reserveList.length > 0) {
            let reserve = reserveList[0];
            let status = create(reserve.name, "预订兵", reserve.level, spawn, reserve.reserveRoom);
            changeMemory(status, reserve, room.memory.reserveList);
        }
    }
}
//防御塔运行逻辑
module.exports.openTower = openTower = (tower) => {
    if (!tower.room.memory.peace) {
        let closestHostile = bestFindClose(tower, tower.room.find(FIND_HOSTILE_CREEPS));
        // 如果附近有敌对单位，攻击它
        if (closestHostile) {
            tower.attack(closestHostile);
            return;
        }
    }
    if (tower.room.memory.repair) {
        // 获取受损的建筑
        let closestDamagedStructure = findRepair(tower.room);
        // 有受损的建筑，维修它
        if (closestDamagedStructure.length > 0) {
            tower.repair(closestDamagedStructure[0]);
        }
    }
}
//传送中枢运行逻辑
module.exports.openLink = openLink = (link) => {
    if (link.room.memory.upLinkId === undefined && link.room.controller.level >= 6) {//找到升级中枢
        let upLinkList = link.room.controller.pos.findInRange(findLink(link.room), 5);
        if (upLinkList && upLinkList.length > 0) {
            link.room.memory.upLinkId = upLinkList[0].id
        }
    }
    if (link.room.memory.mainLinkId === undefined) {//找到核心中枢
        link.room.memory.mainLinkId = link.room.find(FIND_MY_SPAWNS)[0].pos.findClosestByRange(findLink(link.room)).id
    }
    if (link.usedStore() >= 200) {
        //首先判断急需能量
        let mainList = findFreeMainSite(link.room);
        if (link.room.memory.upLinkId && mainList.length === 0) {
            if (link.id === link.room.memory.upLinkId) {//如果自己就是升级Link,不变
                return;
            }
            //基于能量到升级Link
            let upLink = Game.getObjectById(link.room.memory.upLinkId);
            if (upLink.freeStore() >= 200) {
                link.transferEnergy(upLink)
                return;
            }
        }
        //而后再到核心Link
        if (link.id === link.room.memory.mainLinkId || link.id === link.room.memory.upLinkId) {
            return;
        }
        let mainLink = Game.getObjectById(link.room.memory.mainLinkId);
        if (mainLink.freeStore() >= 200) {//有能量转移能量
            link.transferEnergy(mainLink);
        }
    }
}
//交易中心
module.exports.autoMarket = autoMarket = (room) => {
    //购买能源
    // let sourceList = room.find(FIND_SOURCES);
    // if (sourceList.length >= 2 && room.terminal.usedStore() > 10000) {
    //     let allOrder = Game.market.getAllOrders({type: ORDER_SELL, resourceType: RESOURCE_GHODIUM})
    //     for (let order of allOrder) {
    //         if (order.price > 25 && order.amount > 5000) {
    //             let consumeResource = Game.market.calcTransactionCost(1000, room.name, order.roomName);
    //             if (consumeResource < 500) {
    //                 Game.market.deal(order.id, 5000, room.name);
    //                 console.log("卖出1000能量");
    //             }
    //         }
    //     }
    // }
}
//lab自动化流程
module.exports.autoLab = autoLab = (lab) => {
    //强化士兵
    //先检查是否存在强化所需资源
    //存在,检查兵种,生成兵种,并搬运能源
    //搬运成功,强化能源
    //不存在强化,生产化合物
    //查找空容器
    //不存在先搬空
    //放入化合物,查看未满足化合物数量
}
//自动配置建筑行为
module.exports.autoBuild = () => {
    for (let i = 0; i < MyRoomList.length; i++) {//遍历我设定的房间
        let room = Game.rooms[MyRoomList[i]]
        //检测是否存在敌兵
        if (Game.time % 5 === 0) {
            room.memory.peace = room.find(FIND_HOSTILE_CREEPS).length <= 0;
            room.memory.repair = findRepair(room).length > 0;
        }
        let towerList = findTower(room);
        //开启房间内塔
        for (let tower of towerList) {
            openTower(tower);
        }
        let linkList = findLink(room);
        //开启房间内中枢
        for (let link of linkList) {
            openLink(link);
        }
        //开启全部母巢孵化
        if (Game.time % 10 === 0) {
            let spawnList = room.find(FIND_MY_SPAWNS).filter(spawn => !spawn.spawning);
            if (spawnList && spawnList.length > 0) {
                createCreep(spawnList[0]);
            }
        }
        //开启市场查询
        // if (Game.time % 300 === 0) {
        //     if (room.terminal !== undefined) {
        //         autoMarket(room);
        //     }
        // }
    }
}